﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.OracleClient;
using Maticsoft.DBUtility;
using System.Data;
using Xiaov.SmartWeb.Application.Models;

namespace Xiaov.SmartWeb.Application.SZBBTree
{
  public partial class SaveTable : System.Web.UI.Page
  {
    private string tableName = "", dwbm = "", nf = "", yf = "", xml = "";
    XMLVERSION modelXml = null;
    System.Text.StringBuilder zbbmSB = new System.Text.StringBuilder();
    System.Text.StringBuilder zdSB = new System.Text.StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
      string jsonData = "";
      if (Request.Form["tableName"] != null || Request.Form["dwbm"] != null ||
        Request.Form["nf"] != null || Request.Form["yf"] != null)
      {
        tableName = Request.Form["tableName"].ToString();
        dwbm = Request.Form["dwbm"].ToString();
        nf = Request.Form["nf"].ToString();
        yf = Request.Form["yf"].ToString();
        xml = Server.UrlDecode(Request.Form["xml"].ToString());
        //InitVersion();
        modelXml = new XMLVERSION(nf, yf, dwbm, tableName);
        if (Save())
        {
          jsonData = "{\"save\":1}";
        }
        else
        { jsonData = "{\"save\":0}"; }
      }
      else
      {
        jsonData = "{\"save\":0}";
      }
      Response.Write(jsonData);
      Response.ContentType = "text/plain";
      Response.End();

    }

    private bool Save()
    {
      bool flag = false;
      string[] columnList = modelXml.SELECTFIELD.Split(new char[] { ',' });
      System.Text.StringBuilder sql = new System.Text.StringBuilder();
      sql.Append("begin ");
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml);
      XmlNode table = doc.DocumentElement;
      XmlNode modifyRow = table.SelectSingleNode("modifiedRow");
      foreach (XmlNode row in modifyRow.ChildNodes)
      {
        string zbbm = row.Attributes["key"].Value;
        sql.Append("update "); sql.Append(tableName); sql.Append(" set ");
        foreach (XmlNode col in row.ChildNodes)
        {
          string colName = col.Name;
          zdSB.Append(colName + ",");
          zbbmSB.Append(zbbm.Substring(6, 4) + ",");
          string value = string.IsNullOrEmpty(col.InnerText) ? "0" : col.InnerText;
          sql.Append(colName); sql.Append("='"); sql.Append(value); sql.Append("' ");
          if (col != row.ChildNodes[row.ChildNodes.Count - 1])
          { sql.Append(","); }
        }
        sql.Append(" where dwbm='"); sql.Append(dwbm);
        sql.Append("' and nf='"); sql.Append(nf);
        sql.Append("' and yf='"); sql.Append(yf);
        sql.Append("' and zbbm='"); sql.Append(zbbm);
        sql.Append("';");
      }
      sql.Append("null;");
      sql.Append("end;");
      //string s = sql.ToString();
      flag = DbHelperOra.ExecuteSql(sql.ToString()) > 0 ? true : false;
      //string dwParent = DbHelperOra.GetSingle("select f_getparentdw() from dual").ToString();
      //if (dwbm != dwParent)
      //{
      //LoadProc("p_sum");
      //}
      LoadProc("p_compute_sum");
      return flag;
    }

    public bool LoadProc(string ProcdureName)
    {
      OracleParameter[] parameters ={
            new OracleParameter("v_bbbm",OracleType.VarChar,20),
　　     new OracleParameter("v_dwbm",OracleType.VarChar,20),
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2),
            new OracleParameter("c_zbbm",OracleType.Clob),
            new OracleParameter("c_zd",OracleType.Clob)
　　      };
      parameters[0].Value = tableName.ToUpper();
      parameters[1].Value = dwbm.ToUpper();
      parameters[2].Value = nf;
      parameters[3].Value = yf;
      parameters[4].Value = zbbmSB.ToString().Trim(new char[] { ',' });
      parameters[5].Value = zdSB.ToString().Trim(new char[] { ',' });
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure(ProcdureName, parameters); odr.Close();
        return true;
      }
      catch
      {
        return false;
      }
    }
  }
}